<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="zh">
<%@include file="/lyear/common/head.jsp"%>
<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <%@include file="/lyear/common/aside.jsp" %>
        <!--End 左侧导航-->

        <!--头部信息-->
        <%@include file="/lyear/common/top.jsp" %>
        <!--End 头部信息-->

        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-toolbar clearfix">
                                <form id="formId" class="pull-right search-bar" method="get" action="${pageContext.request.contextPath}/proj/getProjByCondition/1/10" role="form">
                                    <div class="input-group">
                                        <div class="input-group-btn">
                                            <input type="hidden" name="searchKey" id="search_key" value="name">
                                            <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                                                    名称 <span h="name"></span>
                                            </button>

                                        </div>
                                        <input type="text" class="form-control" id="search-value" value="${searchValue}" name="searchValue" placeholder="请输入" onkeydown="selectProjByCondition()">
                                    </div>
                                </form>
                                <div class="toolbar-btn-action">
                                    <a class="btn btn-success m-r-5" href="${pageContext.request.contextPath}/proj/getCategoryByCondition"><i class="mdi mdi-check"></i> 新增</a>

                                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal2" data-whatever="@mdo"><i class="mdi mdi-plus"></i>批量新增</button>
                                    <div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel2">
                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                    <h4 class="modal-title" id="exampleModalLabel2">批量新增</h4>
                                                </div>
                                                <div class="modal-body">
                                                    <form id="userRegistForm11"  enctype="multipart/form-data">
                                                        <div class="form-group has-feedback feedback-left">
                                                            <input type="file"  value="" id="projPath">
<%--                                                            <table>请输入xlsx文件磁盘根路径</table><input type="text" id="projPath">--%>
                                                        </div>
                                                        <div class="form-group has-feedback feedback-left">
                                                            <button id="sub2" class="btn btn-block btn-primary" type="button" onclick="registProj()">确认新增</button>
                                                        </div>
                                                    </form>
                                                </div>

                                            </div>
                                        </div>
                                    </div>

                                    <a class="btn btn-warning m-r-5" href="#!" onclick="disabledProjs()"><i class="mdi mdi-block-helper"></i> 禁用</a>
                                    <a class="btn btn-danger" href="#!"  onclick="deleteProjs()"><i class="mdi mdi-window-close" ></i> 删除</a>
                                </div>
                            </div>

                            <div class="card-body">
                                <div class="alert alert-success" role="alert" style="display: none;" id="resSuccessDiv"></div>
                                <div class="alert alert-danger" role="alert" style="display: none;" id="resFailDiv"></div>

                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead>
                                        <tr>
                                            <th>
                                                <label class="lyear-checkbox checkbox-primary">
                                                    <input type="checkbox" name="ids[]" value="${proj.id}" id="check-all"><span></span>
                                                    <input type="checkbox" name="status[]" value="${proj.status}">
                                                </label>
                                            </th>
                                            <th>编号</th>
                                            <th>名称</th>
                                            <th>首图</th>
                                            <th>作者</th>
                                            <th>简介</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <c:forEach items="${pageBean.result}" var="proj">
                                            <tr>
                                                <td>
                                                    <label class="lyear-checkbox checkbox-primary">
                                                        <input type="checkbox" name="ids[]" value="${proj.id}"><span></span>
                                                    </label>
                                                </td>
                                                <td>${proj.id}</td>
                                                <td>${proj.name}</td>
                                                <td>
                                                    <img src="${proj.img}" height="100px" width="100px">
                                                </td>
                                                <td>
                                                    <c:forEach items="${proj.authorListDTOS}" var="authornames">
                                                        ${authornames.nickname}
                                                    </c:forEach>
                                                </td>
                                                <td>${proj.info}</td>

                                                <td>
                                                        <%--     0:正常,1:禁用,2:删除--%>
                                                    <c:if test="${proj.status==0}">
                                                        <span style="color: #0FB25F;">正常</span>
                                                    </c:if>
                                                    <c:if test="${proj.status==1}">
                                                        <span style="color: yellow;">禁用</span>
                                                    </c:if>
                                                    <c:if test="${proj.status==2}">
                                                        <span style="color: red;">删除</span>
                                                    </c:if>
                                                </td>
                                                <td>
                                                    <div class="btn-group">
                                                        <a class="btn btn-xs btn-default" href="${pageContext.request.contextPath}/proj/getProjById/${proj.id}?z=1" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                                                        <a class="btn btn-xs btn-default" href="${pageContext.request.contextPath}/proj/getProjById/${proj.id}" title="查看" data-toggle="tooltip"><i class="mdi mdi-eye"></i></a>
                                                        <a class="btn btn-xs btn-default" href="#!" title="删除" data-toggle="tooltip">
                                                            <i class="mdi mdi-window-close" onclick="deleteProj(${proj.id},${proj.status})"></i>
                                                        </a>
                                                    </div>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                        </tbody>
                                    </table>
                                </div>

                                <%-- 分页--%>
                                <%@include file="/lyear/common/page.jsp" %>
                                <!--End 分页-->

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/main.min.js"></script>

<!--对话框-->
<script src="${pageContext.request.contextPath}/lyear/js/jconfirm/jquery-confirm.min.js"></script>
<!--标签插件-->
<script src="${pageContext.request.contextPath}/lyear/js/jquery-tags-input/jquery.tagsinput.min.js"></script>


<script type="text/javascript">

    /**
     *  禁用：必须所选全部为正常状态，才可禁用
     * */
    function disabledProjs(){
        let projId=[];
        let status = [];
        $("input[name='ids[]']:checked").each(function(){
            projId.push($(this).val());
            status.push($(this).next().next().val());
        })
        let statusYes0=0;
        for (let i = 0; i < status.length; i++) {
            if (status[i]!=0){
                statusYes0++;
            }
        }
        if (statusYes0!=0){
            $.confirm({
                title: '温馨提示',
                content: '请选择正常状态的项目',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        if (projId.length==0){
            $.confirm({
                title: '温馨提示',
                content: '请添加需要禁用的项目',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        $.confirm({
            title: '禁用项目',
            content: '确认禁用吗？',
            type: 'red',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/user/disabledById/" + projId,
                            type:"POST",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.msg);
                                }
                            },
                            dataType: 'json'
                        })
                    }
                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };
    /**
     * 删除：所选必须为正常或者禁用才可删除，如果状态为删除不可再删除
     * */
    function deleteProjs(){
        let projId=[];
        let status = [];
        $("input[name='ids[]']:checked").each(function(){
            projId.push($(this).val());
            status.push($(this).next().next().val());
        })
        let statusYes2=false;
        for (let i = 0; i < status.length; i++) {
            if (status[i]==2){
                statusYes2 = true;
                break;
            }

        }
        if (statusYes2){
            $.confirm({
                title: '温馨提示',
                content: '选择的有已删除状态的项目',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        if (projId.length==0){
            $.confirm({
                title: '温馨提示',
                content: '请添加需要删除的用户',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        $.confirm({
            title: '删除用户',
            content: '确认删除吗？',
            type: 'red',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/proj/removeById/" + projId,
                            type: "DELETE",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.msg);
                                }
                            },
                            dataType: 'json'
                        })
                    }
                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };
    /**
     * 操作单个删除
     * */
    function deleteProj(id,sts) {
        if (sts==2){
            $.confirm({
                title: '温馨提示',
                content: '该项目已被删除',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        $.confirm({
            title: '删除项目',
            content: '确认删除吗？',
            type: 'red',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({

                            url: "${pageContext.request.contextPath}/proj/removeById/" + id,
                            type: "DELETE",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.msg);
                                }

                            },
                            dataType: 'json'
                        })

                    }

                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };

    function selectProjByCondition(e) {
        let evt = window.event || e;
        if (evt.keyCode == 13) { //回车事件
          $("#formId").submit;
        }
    }

    $(function () {
        $('.search-bar .dropdown-menu a').click(function () {
            let field = $(this).data('field') || '';
            $('#search_key').val(field);
            $('#search-btn').html($(this).text() + '<span h="' + field + '"></span>');
        });
    })
    /**
     * 批量添加项目
     * @returns {boolean}
     */
    function registProj() {
        let projPath = $("#projPath").val();

        if (projPath=='') {
            alert("请填写文件磁盘路径！");
            return false;
        }
        $("#sub2").html("正在添加中...");
        $("#sub2").attr("disabled","true");

        $.ajax({
            url: '${pageContext.request.contextPath}/proj/addProjs', /*接口域名地址*/
            data: {"xlsxPath":projPath},
            type: 'POST',
            dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    alert(res.msg);
                    location.reload();
                }
            }
        })
    }
</script>
</body>
</html>